package com.fuxi.cloud.desreport.render.a.br;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fuxi.cloud.common.constant.ExpConstant;
import com.fuxi.cloud.desreport.model.RenderInfo;
import com.fuxi.cloud.desreport.render.handler.CellsRenderHandler;
import com.fuxi.cloud.desreport.render.utils.RegexMatches;
import com.fuxi.cloud.desreport.render.utils.RenderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */

@Component("functionRender")
public class b implements CellsRenderHandler {
    private static final Logger a = LoggerFactory.getLogger(b.class);

    public b() {
    }

    @Override
    public boolean support(JSONObject cell, String text) {
        return RenderUtil.a(text, ExpConstant.FREEMARKER_FUNCTION);
    }

    @Override
    public void beforeRender(RenderInfo renderInfo, JSONObject cell, String text) {
        String var4 = this.a(text);
        cell.put("text", var4);
    }

    private String a(String var1) {
        List var2 = RegexMatches.g(var1);
        Boolean var3 = RenderUtil.b(var1, ExpConstant.FREEMARKER_FUNCTION);
        String var4;
        String var8;
        if (var3) {
            if (ObjectUtil.isNotEmpty(var2) && var2.size() > 1) {
                var4 = RegexMatches.a(var1, 1);
                int var5 = 0;
                String var6 = "";

                for(Iterator var7 = var2.iterator(); var7.hasNext(); ++var5) {
                    var8 = (String)var7.next();
                    if (var5 == 0) {
                        var6 = String.format(var8 + "%s?number");
                    } else {
                        var6 = String.format(var4 + "." + var8 + "%s?number");
                    }

                    var1 = var1.replace(var8, var6);
                }
            } else {
                var4 = RegexMatches.a(var1, 2);
                var1 = var1.replace(var4, var4);
            }
        } else {
            var4 = RegexMatches.a(var1, 1);
            String var9 = RegexMatches.a(var1, 2);
            List var10 = RegexMatches.d(var9);
            if (ObjectUtil.isNotEmpty(var10)) {
                String var11 = (String)var10.get(1);
                var8 = (String)var10.get(2);
                var1 = String.format("${jeecg.%s(list,%s,'%s')}", var11, var4, var8);
            }
        }

        return var1;
    }
}

